﻿using System;
using Framework.MVC;
using Identity.Model;
using Log.Model;
using Framework.MVVM.Model;
using Common.IService;
using Common.Service;
using Log.IService;
using Log.Service;
using Identity.IService;
using Identity.Service;


namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class PasswordMailModel : AsyncModelBase, IDeleteIdentity, INewIdentity, IWriteLog
    {
        #region Field

        private const string m_error = "error";
        private const string m_entry = "entry";

        #endregion Field

        #region Property

        public string Resource { get; set; }

        #endregion Property

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.DeleteIdentity(string.Empty, string.Empty);
        }

        #endregion AsyncModelBase

        #region IDeleteIdentity

        public string IdentityCode { get; set; }

        public string IdentityResource { get; set; }

        public string Source { get; set; }

        public void OnDeleteIdentityComplete(string key, Identity.IService.Entry.IdentityEntry entry)
        {
            IdentityCode = string.Empty;
            IdentityResource = Resource;
            IdentityValue=entry.value;
            this.NewIdentity(string.Empty, string.Empty);
        }

        public void OnDeleteIdentityError(string key)
        {
            OnComplete(m_error, "identity");
            OnEndRequest();
        }

        #endregion IDeleteIdentity

        #region INewIdentity

        public string IdentityValue { get; set; }

        public void OnNewIdentityComplete(string key, Identity.IService.Entry.IdentityEntry entry)
        {
            OnComplete(m_entry, entry);
            this.Log("pwdmail07", entry);
        }

        public void OnNewIdentityError(string key)
        {
            OnComplete(m_error, "identity");
            OnEndRequest();
        }

        #endregion INewIdentity

        #region IWriteLog

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnComplete(m_error, "0002");
            OnEndRequest();
        }

        #endregion IWriteLog
    }
}